﻿using System;
using System.Configuration;
using System.IO;
using System.Web;

namespace Ample.DAL
{
    public abstract class DBBase
    {
        static bool? _EnableDataContextLogging;
        static string _ConnStr;

        protected virtual AmpleDataContext DataContext
        {
            get
            {
                AmpleDataContext context = new AmpleDataContext(this.ConnStr);
                context.DeferredLoadingEnabled = false;
                //context.ObjectTrackingEnabled = false;
                
                if (this.EnableDataContextLogging.Value == true)
                {
                    context.Log = HttpContext.Current.Response.Output;
                }
                //else
                //{
                //    context.Log = new System.IO.StreamWriter(@"E:\AmpleSolution\AmpleWeb\Logs\SQLLogs.log", true) { AutoFlush = true };
                //}
                return context;
            }
        }

        protected virtual bool? EnableDataContextLogging
        {
            get
            {
                if (_EnableDataContextLogging == null)
                {
                    string val = ConfigurationManager.AppSettings["EnableDataContextLogging"];
                    _EnableDataContextLogging = bool.Parse(val);
                }
                return _EnableDataContextLogging;
            }
        }

        protected virtual string ConnStr
        {
            get
            {
                if (_ConnStr == null)
                {
                    _ConnStr = ConfigurationManager.ConnectionStrings["AmpleDBDefConStr"].ConnectionString;
                }
                return _ConnStr;
            }
        }
    }
}
